<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="来源" prop="source">
        <el-select v-model="queryParams.source" placeholder="请选择来源" clearable>
          <el-option
            v-for="dict in dict.type.sl_db_src"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
            </el-form-item>
      <el-form-item label="标题" prop="dbSubject">
        <el-input
          v-model="queryParams.dbSubject"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-row>

    <el-table v-loading="loading" :data="dbinfoList" @selection-change="handleSelectionChange">
      <el-table-column label="接收时间" align="center" prop="receptionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receptionTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_db_src" :value="scope.row.source"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="dbType" width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_event_type" :value="scope.row.dbType"/>
        </template>
      </el-table-column>
      <el-table-column label="提交人" align="center" prop="presenterNick" width="70" />
      <el-table-column label="级别" align="center" prop="dbLevel" width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_event_level" :value="scope.row.dbLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="dbSubject" />
      <el-table-column label="处理人" align="center" prop="transactorsNick" width="70" />
      <el-table-column label="状态" align="center" prop="isDone" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_event_status" :value="scope.row.isDone"/>
        </template>
      </el-table-column>
      <el-table-column label="办理时间" align="center" prop="doneTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.doneTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="70">
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['sl:dbinfo:edit']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详细待办对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="接收时间：">{{ parseTime(form.receptionTime) }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交人：">{{ form.presenterNick }}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="来源：">{{ sourceGroupFormat(form) }} </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="类型：" >{{ typeGroupFormat(form) }} </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="级别：">{{ levelGroupFormat(form) }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内容：">{{ eventInfo.eventContent }}</el-form-item>
            </el-col>
          </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="fnIsDoneForm" v-if="form.isDone!='2'">办理完成</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <Rworkinfo ref="rworkinfo" @onApprove="handleIsApprove" ></Rworkinfo>
    <Oddjobinfo ref="oddjobinfo" @onApprove="handleIsApprove" ></Oddjobinfo>
  </div>
</template>

<script>
import { listDbinfo, getDbinfo, delDbinfo, addDbinfo, updateDbinfo, doneDbinfo } from "@/api/sl/dbinfo";
import Rworkinfo from "./rworkinfo";
import Oddjobinfo from "./oddjobinfo";

export default {
  name: "Dbinfo",
  dicts: ['sl_event_level', 'sl_event_type', 'sl_event_status','sl_db_src'],
  components: {Rworkinfo,Oddjobinfo},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待办表格数据
      dbinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //计件报工弹出层
      jjbgOpen: false,
      jjbgTitle:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dbType: null,
        dbLevel: null,
        dbSubject: null,
        isDb:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      eventInfo:{},
    };
  },
  created() {

    //根据url判断是待办页面还是已办页面
   if(this.$route.query.isDb){
     this.queryParams.isDb = this.$route.query.isDb;
   }

  this.getList();


  },
  methods: {
    /** 查询待办列表 */
    getList() {
      this.loading = true;
      listDbinfo(this.queryParams).then(response => {
        this.dbinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dbId: null,
        receptionTime: null,
        dbType: null,
        presenterNick: null,
        presenterUser: null,
        dbLevel: null,
        dbSubject: null,
        transactorsNick: null,
        transactorsUser: null,
        isDone: null,
        isDb: null,
        sourceId: null,
        source: null,
        doneTime: null,
        reviewsTime: null,
        checkTime: null,
        overTime: null,
        rwUser: null,
        rwDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dbId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待办";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dbId = row.dbId || this.ids
      getDbinfo(dbId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改待办";
      });
    },
    /** 查看详细按钮操作 */
    handleDetail(row) {
      this.reset();
      const dbId = row.dbId || this.ids
      getDbinfo(dbId).then(response => {
        this.form = response.data;
        if(this.form.source==='jjbg'){
          this.$refs.rworkinfo.show(this.form.dbId,this.form.source,this.form.sourceId,this.queryParams.isDb);
        }else if(this.form.source==='lgbg'){
          this.$refs.oddjobinfo.show(this.form.dbId,this.form.source,this.form.sourceId,this.queryParams.isDb);
        }else{
          this.open = true;
          this.title = "详细";
          this.eventInfo = response.hbInfo||{};
        }


      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dbId != null) {
            updateDbinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDbinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 已办理按钮 */
    fnIsDoneForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dbId != null) {
            doneDbinfo(this.form.dbId).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dbIds = row.dbId || this.ids;
      this.$modal.confirm('是否确认删除待办编号为"' + dbIds + '"的数据项？').then(function() {
        return delDbinfo(dbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/dbinfo/export', {
        ...this.queryParams
      }, `dbinfo_${new Date().getTime()}.xlsx`)
    },
    // 来源字典翻译
    sourceGroupFormat(row, column) {
      return this.selectDictLabel(this.dict.type.sl_db_src, row.source);
    },
    //类型字典翻译
    typeGroupFormat(row, column) {
      return this.selectDictLabel(this.dict.type.sl_event_type, row.dbType);
    },
    //汇报级别字典翻译
    levelGroupFormat(row, column) {
      return this.selectDictLabel(this.dict.type.sl_event_level, row.dbLevel);
    },
    handleIsApprove(){
      this.$modal.msgSuccess("审核完成");
      this.getList();
    }
  }
};
</script>
